/*
 * Copyright 2011 JessTsai(simpleisgood1121@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleanform.gwt.chinesechess.shared.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class GameHelper {
	public static boolean hasGamePlayer(ChessGame game) {
		return game.getPlayerRed() != null || game.getPlayerBlack() != null;
	}

	public static boolean checkGamePlayer(ChessGame game, String player) {
		return player.equals(game.getPlayerRed()) || player.equals(game.getPlayerBlack());
	}

	public static boolean checkGameReady(ChessGame game) {
		return game.getPlayerRed() != null && game.getPlayerBlack() != null;
	}

	public static boolean checkGameStart(ChessGame game) {
		return game.getChessMoves() != null && game.getChessMoves().size() > 0;
	}

	public static List<String> allPlayers(ChessGame game) {
		List<String> players = new ArrayList<String>();
		if (game.getPlayerRed() != null) {
			players.add(game.getPlayerRed());
		}
		if (game.getPlayerBlack() != null) {
			players.add(game.getPlayerBlack());
		}
		if (game.getObservers() != null) {
			players.addAll(game.getObservers());
		}
		return players;
	}

	public static boolean playerExit(ChessGame game, String clientid) {
		if (clientid.equals(game.getPlayerRed())) {
			game.setPlayerRed(null);
			return true;
		} else if (clientid.equals(game.getPlayerBlack())) {
			game.setPlayerBlack(null);
			return true;
		} else if (game.getObservers() != null) {
			return game.getObservers().remove(clientid);
		} else {
			return false;
		}
	}

	public static boolean playerJoin(ChessGame game, String player) {
		boolean joined = false;
		if (game.getPlayerRed() == null) {
			game.setPlayerRed(player);
			joined = true;
		} else if (game.getPlayerBlack() == null) {
			game.setPlayerBlack(player);
			joined = true;
		}
		// player join and ready
		if (joined) {
			restart(game);
			// check observer join
			if (game.getObservers() != null && game.getObservers().contains(player)) {
				game.getObservers().remove(player);
			}
		}
		return joined;
	}

	public static void restart(ChessGame game) {
		game.setActive(ChessGame.ACTIVE_RED);
		game.setChessMoves(null);
	}

	public static ChessGame retrieveGameIdentity(ChessGame g) {
		ChessGame game = new ChessGame();
		game.setGameid(g.getGameid());
		return game;
	}

	public static ChessGame retrieveGameInfo(ChessGame g) {
		ChessGame game = new ChessGame();
		game.setGameid(g.getGameid());
		game.setDesc(g.getDesc());
		game.setPlayerBlack(g.getPlayerBlack());
		game.setPlayerRed(g.getPlayerRed());
		game.setActive(g.getActive());
		return game;
	}

	public static boolean hasObserver(ChessGame game, String player) {
		return game.getObservers() == null ? false : game.getObservers().contains(player);
	}

	public static void resetPlayerRed(ChessGame game, String player) {
		game.setPlayerRed(player);
		if (player == null) {
			restart(game);
		}
	}
	
	public static void resetPlayerBlack(ChessGame game, String player) {
		game.setPlayerBlack(player);
		if (player == null) {
			restart(game);
		}
	}
	
	public static boolean addObserver(ChessGame game, String clientid) {
		if (game.getObservers() == null) {
			game.setObservers(new ArrayList<String>());
		}
		if (game.getObservers().size() <ChessGame.MAX_GAME_OBSERVERS) {
			game.getObservers().add(clientid);
			return true;
		} else {
			return false;
		}
	}

	public static void applyMove(ChessGame game, ChessMove move) {
		if (game.getChessMoves() == null) {
			game.setChessMoves(new ArrayList<ChessMove>());
		}
		game.getChessMoves().add(move);
		game.setActive(-game.getActive());
	}
}
